import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Index from '../components/Index.vue'
import MovieView from '../components/MovieView.vue'
import Register from '../components/Register.vue'

Vue.use(VueRouter)

const routes = [
    {path:'/register',component: Register},
    {path:'/login',component: Login},
    {path:'/',redirect:'/login'},
    {
      path:'/home',
      component: Home,
      redirect:'/index',
      children:[
        {path:'/index',component: Index},
        {path:'/movieview',component: MovieView}
      ]
    }
]

const router = new VueRouter({
  routes
})

//路由导航守卫，防止没有登录就可以直接访问其他页面
// router.beforeEach((to,from,next)=>{
//   //to 将要访问的路径
//   // from 代表从哪个路径跳转而来
//   // next是一个函数，表示放行
//   // next() 放行；next('login') 强制跳转到login页面

//   if(to.path==='/login') return next();
//   //获取token
//   const token=window.sessionStorage.getItem('token');
//   if(!token) return next('/login');
//   next();
// })

export default router
